#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
map<int, int> mp;

ll get(ll b, ll c) {
    ll d = b * b - 4 * c;
    if(d < 0) return 0;
    ll x1 = (b - sqrt(d)) / 2;
    ll x2 = (b + sqrt(d)) / 2;
    if(x1 + x2 != b || x1 * x2 != c) return 0;
    if(x1 == x2) return (ll)mp[x1] * (mp[x1] - 1);
    else return (ll)mp[x1] * mp[x2];
}

void solve() {
    ll n; cin >> n;
    mp.clear();
    for(int i = 0; i < n; i++) {
        int x; cin >> x;
        mp[x]++;
    }
    int q; cin >> q;
    while(q--) {
        ll x, y;
        cin >> x >> y;
        cout << get(x, y) << ' ';
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}